<?php
/**
 * ORA 模板在线安装 功能类库
 *  @author lan7
 *  @copyright lan7.net
 *
 */
class tpl_lib {

	/**
	 * 获取官网发布的模板数据
	 *
	 * @return unknown
	 */
	static function getAllTpl(){
		$xdata = array();
		if(preg_match('/lan7\.net/i',HTTP_HOST)){
			$xdata = array();
			$q = $GLOBALS['db']->query("select * from ".tn('theme')." order by Id desc");
			while($row = $GLOBALS['db']->fetch_array($q)){
				$xdata[$row['p_code']] = $row;
			}
		}else{
			$str = file_get_contents(ORA_UPDATE."/update/tplXml");
			if(sizeof($str)>0){
				$xobj = @simplexml_load_string($str);
				$count = sizeof($xobj->tpl);
				$xdata = array();
				for($j=0;$j<$count;$j++){
					$tmp = array();
					foreach($xobj->tpl[$j]->attributes() as $a => $b) {
						$tmp[(string)$a] = (string)$b;
					}
					$xdata[$tmp['theme_code']] = $tmp;
				}
			}
		}
		return $xdata;
	}

	/**
	 * 删除安装的模板
	 *
	 * @param unknown_type $code
	 * @return unknown
	 */
	static function delTpl($code){
		// 排除非法字符可能导致的误删
		if(preg_match('/^[a-zA-Z0-9\-]+$/i',$code)){
			$zipPath = UPLOAD_PATH."zip/".$code.".zip";
			$filePath = THEMES_PATH.$code."/";
			io::remove($zipPath);
			io::removeDir($filePath);
			return true;
		}
		return false;
	}
	
	/**
	 * 模板入库
	 *
	 * @param unknown_type $xml
	 * @return unknown
	 */
	static function insertTpl($xml){
		if(is_file($xml)){
			$strBuf = file_get_contents($xml);
			if(sizeof($strBuf)>0){
				$xobj = simplexml_load_string($strBuf);
				if($xobj){
					$sql = "insert into ".tn('theme')."(theme_name,theme_pic,theme_author,theme_homepage,theme_file ";
					$sql .=",theme_date,theme_keywords,theme_color,theme_type,theme_style,theme_code,ver) ";
					$sql.=" values('{$xobj->theme_name }','{$xobj->theme_pic }','{$xobj->theme_author }',";
					$sql.="'{$xobj->theme_homepage}','{$dataFilePath}',".time().",'{$xobj->theme_keywords }',";
					$sql.="'{$xobj->theme_color }',0,'{$xobj->theme_style }','{$xobj->theme_code }','{$xobj->theme_ver}')";
					if($GLOBALS['db']->execute($sql)){
						return true;
					}
				}
			}
		}
		return false;
	}
}
?>